﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;


namespace Economo.Services.DataContext
{
    public class BanksDataContext : IDisposable 
    {
        DataClassesDataContext _context = new DataClassesDataContext(ConfigurationManager.ConnectionStrings["Economo"].ConnectionString);           
        public List<Bank> GetBanksByDescription(string description)
        {
            try
            {                
                var banks = from b in _context.Banks
                            where b.Description.Contains(description)
                            orderby b.Description
                            select b;
                List<Bank> result = new List<Bank>(banks);
                return result;
            }
            catch (Exception ex)
            {                
                ExceptionHandling.ManageExceptions.ManageDataAccessException(new ExceptionHandling.DataAccessException("Error getting banks by description.", ex));
                return null;
            }            
        }
        public Bank GetBankById(byte id)
        {
            try
            {
                Bank bank = (from b in _context.Banks
                            where b.Id == id
                            select b).FirstOrDefault<Bank>();                
                return bank;
                            
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageDataAccessException(new ExceptionHandling.DataAccessException("Error getting banks by id.", ex));
                return null;
            } 
        }


        public Byte InsertBank(Bank newBank)
        {
            try
            {

                InsertBankResult result = _context.InsertBank(newBank.Description, newBank.Picture).FirstOrDefault<InsertBankResult>();
                if (result.Id.HasValue)
                    return result.Id.Value;
                else
                    throw new ExceptionHandling.DataAccessException("Impossible to retrieve autogenerated id when inserting new row.");
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageDataAccessException(new ExceptionHandling.DataAccessException("Error inserting new bank.", ex));
                return 0;
            }
        }


        public void UpdateBank(Bank bank)
        {
            try
            {                                
                Bank updated = (from b in _context.Banks
                               where b.Id == bank.Id
                                select b).First() ;
                if (updated != null)
                {
                    updated.Description = bank.Description;
                    updated.Picture = bank.Picture;
                    _context.SubmitChanges(System.Data.Linq.ConflictMode.FailOnFirstConflict);
                }
                else                                    
                    throw new ExceptionHandling.DataAccessException("Bank not found with Id=" + bank.Id.ToString());
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageDataAccessException(new ExceptionHandling.DataAccessException("Error updating bank.",ex));
            }
        }
        public void DeleteBank(Bank bank)
        {
            try
            {
                _context.Banks.DeleteOnSubmit(bank);                                
                _context.SubmitChanges(System.Data.Linq.ConflictMode.FailOnFirstConflict);                
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageDataAccessException(new ExceptionHandling.DataAccessException("Error deleting bank.",ex));
            }
        }
        public void DeleteBank(byte id)
        {
            try
            {
                Bank bank = GetBankById(id);
                if (bank != null)
                    DeleteBank(bank);
                else
                    throw new ExceptionHandling.DataAccessException("Bank to delete not found.");
            }
            catch (Exception ex)
            {
                ExceptionHandling.ManageExceptions.ManageDataAccessException(new ExceptionHandling.DataAccessException("Error deleting bank.", ex));
            }
        }



        public void Dispose()
        {            
            _context.Dispose();
        }
    }
}
